from itsdangerous import TimedJSONWebSignatureSerializer as Serializer,BadSignature,SignatureExpired
from mall import settings


# 1.当有几行代码实现了一个功能，我们就可以将代码进行封装和抽取
# 2. 以后工作中，只要第二次遇到了重复的代码直接 封装和抽取

def generic_openid_token(openid):
    # 1.创建序列化器
    serializer=Serializer(settings.SECRET_KEY,3600)
    # 2.对openid进行处理
    token=serializer.dumps({'openid':openid})
    # 3. 因为 dumps返回的是二进制
    openid_token=token.decode()

    return openid_token




def check_openid_token(access_token):

    serializer = Serializer(settings.SECRET_KEY, 3600)

    try:
        dict=serializer.loads(access_token)

    except BadSignature:
        return None

    else:
        openid=dict.get('openid')
        return openid